package com.xuyanze.es.model;

import com.xuyanze.es.enums.EsJoinType;
import com.xuyanze.es.enums.EsKeyWord;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;

import java.util.Collection;
import java.util.Objects;

/**
 * @author ：lzy
 * @date ：2023-02-10
 * @description：
 */
public class EsSegment {

    /**
     * 连接类型
     */
    private EsJoinType joinType;

    /**
     * 关键字查询
     */
    private EsKeyWord keyWord;

    /**
     * 子查询
     */
    private BoolQueryBuilder childrenQueryBuilder;

    /**
     * 查询字段
     */
    private String field;
    private Object val;
    private Object[] vals;

    /**
     * between 使用
     */
    private Object from;
    private Object to;

    /**
     * 提升或降低一个语句相对权重
     */
    private float boost = 1f;



    public EsSegment(){}

    private EsSegment(EsJoinType joinType, EsKeyWord keyWord, String field, Object val) {
        this.joinType = joinType;
        this.keyWord = keyWord;
        this.field = field;
        this.val = val;
    }

    private EsSegment(EsJoinType joinType, EsKeyWord keyWord, String field, Object[] vals) {
        this.joinType = joinType;
        this.keyWord = keyWord;
        this.field = field;
        this.val = val;
        this.vals = vals;
    }

    private EsSegment(EsJoinType joinType, EsKeyWord keyWord, String field, Object from,Object to) {
        this.joinType = joinType;
        this.keyWord = keyWord;
        this.field = field;
        this.from = from;
        this.to = to;
    }

    private EsSegment(String field, Object val) {
        this.field = field;
        this.val = val;
    }

    private EsSegment(EsJoinType joinType, EsKeyWord keyWord) {
        this.joinType = joinType;
        this.keyWord = keyWord;
    }

    private EsSegment(EsJoinType joinType) {
        this.joinType = joinType;
    }

    public EsSegment(EsJoinType joinType, EsKeyWord keyWord, BoolQueryBuilder childrenQueryBuilder) {
        this.joinType = joinType;
        this.keyWord = keyWord;
        this.childrenQueryBuilder = childrenQueryBuilder;
    }

    public EsSegment(EsJoinType joinType, EsKeyWord keyWord, String field) {
        this.joinType = joinType;
        this.keyWord = keyWord;
        this.field = field;
    }

    public static EsSegment of(String field, Object val){
        return new EsSegment(field,val);
    }

    public static EsSegment of(EsJoinType joinType){
        return new EsSegment(joinType);
    }

    public static EsSegment of(EsJoinType joinType, EsKeyWord keyWord){
        return new EsSegment(joinType,keyWord);
    }

    public static EsSegment of(EsJoinType joinType, EsKeyWord keyWord, BoolQueryBuilder childrenQueryBuilder){
        return new EsSegment(joinType,keyWord,childrenQueryBuilder);
    }

    public static EsSegment of(EsJoinType joinType, EsKeyWord keyWord, String field){
        return new EsSegment(joinType,keyWord,field);
    }

    public static EsSegment of(EsJoinType joinType, EsKeyWord keyWord,String field, Object val){
        return new EsSegment(joinType,keyWord,field,val);
    }

    public static EsSegment of(EsJoinType joinType, EsKeyWord keyWord,String field, Object[] vals){
        return new EsSegment(joinType,keyWord,field,vals);
    }

    public static EsSegment of(EsJoinType joinType, EsKeyWord keyWord,String field, Object from,Object to){
        return new EsSegment(joinType,keyWord,field,from,to);
    }


    public EsJoinType getJoinType() {
        return joinType;
    }

    public void setJoinType(EsJoinType joinType) {
        this.joinType = joinType;
    }


    public BoolQueryBuilder getChildrenQueryBuilder() {
        return childrenQueryBuilder;
    }

    public void setChildrenQueryBuilder(BoolQueryBuilder childrenQueryBuilder) {
        this.childrenQueryBuilder = childrenQueryBuilder;
    }

    public EsKeyWord getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(EsKeyWord keyWord) {
        this.keyWord = keyWord;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public Object getVal() {
        return val;
    }

    public void setVal(Object val) {
        this.val = val;
    }

    public QueryBuilder bindQuery() {
        return this.keyWord.bindQuery(this);
    }

    public Object getFrom() {
        return from;
    }

    public void setFrom(Object from) {
        this.from = from;
    }

    public Object getTo() {
        return to;
    }

    public void setTo(Object to) {
        this.to = to;
    }

    public float getBoost() {
        return boost;
    }

    public EsSegment setBoost(float boost) {
        this.boost = boost;
        return this;
    }

    public Object[] getVals() {
        return vals;
    }
}
